package Algorithm.StackAndQueue;

import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

// 左的思路
public class CatDogQueue2 {

    class PetEnterQueue {
        private Pet pet;
        private long count;

        public PetEnterQueue(Pet pet, long count) {
            this.pet = pet;
            this.count = count;
        }

        public Pet getPet() {
            return this.pet;
        }

        public long getCount() {
            return this.count;
        }

        public String getPetEnterType() {
            return this.pet.getPetType();
        }
    }

    private Queue<PetEnterQueue> dogQ;
    private Queue<PetEnterQueue> catQ;
    private long count;

    public CatDogQueue2() {
        this.dogQ = new LinkedList<>();
        this.catQ = new LinkedList<>();
        this.count = 0;
    }

    public void add(Pet pet) {
        if (pet.getPetType().equals("dog")) {
            this.dogQ.offer(new PetEnterQueue(pet, System.currentTimeMillis()));
        } else if (pet.getPetType().equals("cat")) {
            this.catQ.offer(new PetEnterQueue(pet, System.currentTimeMillis()));
        } else {
            throw new RuntimeException("not dog or cat.");
        }
    }

    public Pet pollAll() {
        if (!this.dogQ.isEmpty() && !this.catQ.isEmpty()) { // 比较
            if (this.dogQ.peek().getCount() <= this.catQ.peek().getCount()) {
                return this.dogQ.poll().pet;
            } else {
                return this.catQ.poll().pet;
            }
        } else if (!this.dogQ.isEmpty()) {
            return this.dogQ.poll().pet;
        } else if (!this.catQ.isEmpty()) {
            return this.catQ.poll().pet;
        } else {
            throw new RuntimeException("no dog or cat");
        }
    }

    public Dog pollDog() {
        if (!this.dogQ.isEmpty()) {
            return (Dog)this.dogQ.poll().pet;
        } else {
            throw new RuntimeException("no dog");
        }
    }

    public Cat pollCat() {
        if (!this.catQ.isEmpty()) {
            return (Cat)this.catQ.poll().pet;
        } else {
            throw new RuntimeException("no cat");
        }
    }


}
